<!-- 编辑/新增 对话框组件 -->
<template>
  <!-- 父组件:xxx.sync=""的sync相当于双向绑定v-model,  @close发射update:xxx出去双向绑定 -->
  <el-dialog
    :title="form.id? '编辑部门' : '新增部门'"
    :visible="dialogVisible"
    width="30%"
    @close="$emit('update:dialogVisible', false)"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <!-- size输入框尺寸 -->
        <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>

      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <!-- select选择器 -->
        <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <!-- label显示的文本 value是id -->
          <el-option v-for="v in simpleList" :key="v.id" :label="v.username" :value="v.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
      </el-form-item>
      <el-form-item>
        <!-- 按钮  -- el-row相当于鸿蒙的Row组件 -- -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="onSubmit">确定</el-button>
            <el-button size="mini" @click="$emit('update:dialogVisible', false)">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentAPI, editDepartmentAPI, getDepartmentAPI, getDepartmentDetailAPI, getSimpleAPI } from '@/api/department'

export default {
  props: {
    dialogVisible: Boolean,
    currentPid: { // 1.3当前部门的id
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      simpleList: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '' // 部门负责人id
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            required: true,
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              /*
                let list = await getDepartmentAPI()
                // 如果是编辑时, 比较之前, 要先把自身这个部门, 从数据中剔除掉
                if (this.formData.id) {
                  // 编辑 时 排除自身
                  list = list.filter(v => v.id !== this.formData.id)
                }
              */
              const res = await getDepartmentAPI()
              const list = res.map(v => v.name)
              if (this.form.id) {
                // 找到当前部门名称的下标, 然后删除, 重新生成一个新数组, 绕开当前的部门名称校验
                list.splice(list.indexOf(this.form.name), 1)
              }
              list.includes(value) ? callback(new Error('部门名称已存在')) : callback()
            }
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          {
            required: true,
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              const res = await getDepartmentAPI()
              // toUpperCase() 把字符串转换为大写再笔记
              const list = res.map(v => v.code.toUpperCase())
              if (this.form.id) {
                // 找到当前部门编码的下标, 然后删除, 重新生成一个新数组, 绕开当前的部门名称校验
                list.splice(list.indexOf(this.form.code), 1)
              }
              list.includes(value.toUpperCase()) ? callback(new Error('部门编码已存在')) : callback()
            }
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍长度为1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ]
      }

    }
  },
  watch: {
    // 监听对话框隐藏: 重置表单与重置校验
    dialogVisible(newVal) {
      if (!newVal) {
        this.$refs.form.resetFields()
        // 手动重置掉id,解决一直显示编辑的bug
        this.form = {
          name: '',
          code: '',
          introduce: '',
          managerId: ''
        }
      }
    }
  },
  created() {
    this.getSimple()
  },
  methods: {
    async onSubmit() {
      // 校验
      await this.$refs.form.validate()
      // 把form表单+当前部门id
      const obj = {
        ...this.form,
        pid: this.currentPid
      }

      /* 编辑 */
      if (this.form.id) {
        await editDepartmentAPI(this.form) // 因为打开对话框时,已传递id发请求回显-数据存储在form表单中
      } else {
        /* 新增 */
        await addDepartmentAPI(obj)
      }
      this.$message.success(this.form.id ? '编辑成功' : '新增成功')
      this.$emit('update:dialogVisible', false) // 隐藏对话框
      this.$emit('refresh') // 子传父刷新页面
    },
    // 获取-部门负责人列表
    async getSimple() {
      this.simpleList = await getSimpleAPI()
    },
    // 获取-部门详情-回显
    async getDepartmentDetail(id) {
      this.form = await getDepartmentDetailAPI(id)
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
